package com.cyparty.mac.quaro.main;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.IdRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.cyparty.mac.quaro.R;
import com.cyparty.mac.quaro.find.FindFragment;
import com.cyparty.mac.quaro.home.HomeFragment;
import com.cyparty.mac.quaro.message.MessageFragment;
import com.cyparty.mac.quaro.more.MoreFragment;
import com.cyparty.mac.quaro.notification.NotificationFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnMenuTabClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainScreenActivity extends AppCompatActivity {
    private BottomBar mBottomBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        ButterKnife.bind(this);

        mBottomBar = BottomBar.attach(this, savedInstanceState);
        mBottomBar.useFixedMode();
        mBottomBar.setItems(R.menu.main_bottom_bar);
        mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
            @Override
            public void onMenuTabSelected(@IdRes int menuItemId) {
                if (menuItemId == R.id.action_home) {
                    // The user selected item number one.
                    setHomeFragment();
                }
                if (menuItemId == R.id.action_find) {
                    setFindFragment();
                }
                if (menuItemId == R.id.action_notification) {
                    setNotificationFragment();
                }
                if (menuItemId == R.id.action_message) {
                    setMessageFragment();
                }
                if (menuItemId == R.id.action_more) {
                    setMoreFragment();
                }
            }

            @Override
            public void onMenuTabReSelected(@IdRes int menuItemId) {
                if (menuItemId == R.id.action_home) {
                    // The user reselected item number one, scroll your content to top.
                }
            }
        });

        // Setting colors for different tabs when there's more than three of them.
        // You can set colors for tabs in three different ways as shown below.
        mBottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
        mBottomBar.mapColorForTab(1, 0xFF5D4037);
        mBottomBar.mapColorForTab(2, "#7B1FA2");
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mBottomBar.onSaveInstanceState(outState);
    }

    public void setHomeFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        HomeFragment home = (HomeFragment) supportFragmentManager.findFragmentByTag("home");
        hideAllFragments(supportFragmentManager, supportFragmentManager.getFragments());
        if (home == null) {
            HomeFragment homeFragment = HomeFragment.newInstance();
            supportFragmentManager.beginTransaction().add(R.id.fragment, homeFragment, "home").commit();
        } else {
            supportFragmentManager.beginTransaction().show(home).commit();
        }
    }

    public void setFindFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FindFragment find = (FindFragment) supportFragmentManager.findFragmentByTag("find");
        hideAllFragments(supportFragmentManager, supportFragmentManager.getFragments());
        if (find == null) {
            FindFragment findFragment = FindFragment.newInstance();
            supportFragmentManager.beginTransaction().add(R.id.fragment, findFragment, "find").commit();
        } else {
            supportFragmentManager.beginTransaction().show(find).commit();
        }
    }

    public void setNotificationFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        NotificationFragment notification = (NotificationFragment) supportFragmentManager.findFragmentByTag("notification");
        hideAllFragments(supportFragmentManager, supportFragmentManager.getFragments());
        if (notification == null) {
            NotificationFragment notificationFragment = NotificationFragment.newInstance();
            supportFragmentManager.beginTransaction().add(R.id.fragment, notificationFragment, "notification").commit();
        } else {
            supportFragmentManager.beginTransaction().show(notification).commit();
        }
    }

    public void setMessageFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        MessageFragment message = (MessageFragment) supportFragmentManager.findFragmentByTag("message");
        hideAllFragments(supportFragmentManager, supportFragmentManager.getFragments());
        if (message == null) {
            MessageFragment messageFragment = MessageFragment.newInstance();
            supportFragmentManager.beginTransaction().add(R.id.fragment, messageFragment, "message").commit();
        } else {
            supportFragmentManager.beginTransaction().show(message).commit();
        }
    }

    public void setMoreFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        MoreFragment more = (MoreFragment) supportFragmentManager.findFragmentByTag("more");
        hideAllFragments(supportFragmentManager, supportFragmentManager.getFragments());
        if (more == null) {
            MoreFragment moreFragment = MoreFragment.newInstance();
            supportFragmentManager.beginTransaction().add(R.id.fragment, moreFragment, "more").commit();
        } else {
            supportFragmentManager.beginTransaction().show(more).commit();
        }
    }

    private void hideAllFragments(FragmentManager fragmentManager, List<Fragment> list) {
        if (list == null || list.isEmpty()) return;
        for (Fragment fragment : list) {
            fragmentManager.beginTransaction().hide(fragment).commit();
        }
    }
}
